package com.business.api.official.toursism.websitenews.controller;

import com.business.api.official.toursism.websitenews.dto.WebsiteNewsApiDTO;
import com.business.api.official.toursism.websitenews.from.WebsiteNewsApiSaveFrom;
import com.business.api.official.toursism.websitenews.from.WebsiteNewsApiUpdateFrom;
import com.business.api.official.toursism.websitenews.manager.WebsiteNewsApiManager;
import com.business.api.official.toursism.websitenews.query.WebsiteNewsApiQuery;
import com.business.api.official.toursism.websitenews.query.WebsiteNewsApiPageQuery;
import com.business.api.official.toursism.websitenews.service.WebsiteNewsApiService;
import com.business.api.official.toursism.websitenews.vo.WebsiteNewsApiBriefPageVO;
import com.business.api.official.toursism.websitenews.vo.WebsiteNewsApiDetailPageVO;
import com.business.api.official.toursism.websitenews.vo.WebsiteNewsApiDetailVO;
import com.business.api.official.toursism.websitenews.vo.WebsiteNewsApiDetailListVO;
import com.business.api.official.toursism.websitenews.vo.data.WebsiteNewsApiDetailData;
import com.business.system.official.toursism.websitenews.entity.WebsiteNews;
import com.seed.core.exception.ManagerException;
import com.seed.core.exception.ServiceException;
import com.seed.core.pojo.controller.SeedApiController;
import com.seed.core.pojo.dto.SeedMessageDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @description 乌镇新闻
 * @author CodeGenerator
 * @date   2025-01-25
 * @version V1.0
 */
@Slf4j
@RestController(value = "websiteNewsApiController")
@Api(value = "websiteNewsController", tags = "乌镇新闻")
@RequestMapping("/api/v1/toursism/websiteNews")
public class WebsiteNewsApiController extends SeedApiController<WebsiteNewsApiService, WebsiteNewsApiManager, WebsiteNews> {

    @RequestMapping(method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "保存")
    public WebsiteNewsApiDetailVO save(@RequestBody @ApiParam(value = "保存表单", required = true) WebsiteNewsApiSaveFrom websiteNewsFrom) {
        log.info("[api-v1]WebsiteNewsController.save,保存");

        WebsiteNewsApiDetailVO result = new WebsiteNewsApiDetailVO();
        if (StringUtils.isEmpty(websiteNewsFrom)) {
            result.setCode("A0410");
            result.setMessage("表单不能为空!");
            result.setSuccess(false);
            return result;
        }
        WebsiteNewsApiDTO apiDTO;
        try {
            apiDTO = baseService.save(websiteNewsFrom);
        } catch (ServiceException e) {
            log.error("[api-v1]WebsiteNewsController.save ", e);
            result.setCode("A0500");
            result.setSuccess(false);
            result.setMessage("保存失败");
            return result;
        }
        if (!apiDTO.getSuccess()) {
            result.setCode(apiDTO.getCode());
            result.setSuccess(false);
            result.setMessage(apiDTO.getMessage());
            return result;
        }
        WebsiteNewsApiDetailData data;
        try {
            data = baseManager.convertApiDetailData(apiDTO.getWebsiteNews());
        } catch (ManagerException e) {
            log.error("[api-v1]WebsiteNewsController.save ", e);
            result.setCode("A0500");
            result.setSuccess(false);
            result.setMessage("保存失败");
            return result;
        }
        result.setData(data);
        return result;
    }

    @RequestMapping(method = RequestMethod.PATCH)
    @ResponseBody
    @ApiOperation(value = "不完全更新,更新from中不为空的值（空值不会被更新）")
    public WebsiteNewsApiDetailVO updateSelective(@RequestBody @ApiParam(value = "更新表单", required = true) WebsiteNewsApiUpdateFrom websiteNewsFrom) {
        log.info("[api-v1]WebsiteNewsController.updateSelective,不完全更新,更新from中不为空的值（空值不会被更新）");

        WebsiteNewsApiDetailVO result = new WebsiteNewsApiDetailVO();
        if (StringUtils.isEmpty(websiteNewsFrom)) {
            result.setCode("A0410");
            result.setMessage("更新信息不能为空!");
            result.setSuccess(false);
            return result;
        }
        if (StringUtils.isEmpty(websiteNewsFrom.getId())) {
            result.setCode("A0410");
            result.setMessage("id信息不能为空!");
            result.setSuccess(false);
            return result;
        }
        WebsiteNewsApiDTO apiDTO;
        try {
            apiDTO = baseService.updateSelectiveById(websiteNewsFrom);
        } catch (ServiceException e) {
            log.error("[api-v1]WebsiteNewsController.updateSelective ", e);
            result.setCode("A0500");
            result.setSuccess(false);
            result.setMessage("更新失败");
            return result;
        }
        if (!apiDTO.getSuccess()) {
            result.setCode(apiDTO.getCode());
            result.setSuccess(false);
            result.setMessage(apiDTO.getMessage());
            return result;
        }
        WebsiteNewsApiDetailData data;
        try {
            data = baseManager.convertApiDetailData(websiteNewsFrom.getId());
        } catch (ManagerException e) {
            log.error("[api-v1]WebsiteNewsController.updateSelective ", e);
            result.setCode("A0500");
            result.setSuccess(false);
            result.setMessage("更新失败");
            return result;
        }
        result.setData(data);
        return result;
    }

    @RequestMapping(value = "/{ids}", method = RequestMethod.DELETE)
    @ResponseBody
    @ApiOperation(value = "删除,支持ids")
    public WebsiteNewsApiDetailVO delete(@PathVariable @ApiParam(value = "主键id", required = true) String ids) {
        log.info("[api-v1]WebsiteNewsController.delete,删除");

        WebsiteNewsApiDetailVO result = new WebsiteNewsApiDetailVO();
        if (StringUtils.isEmpty(ids)) {
            result.setCode("A0410");
            result.setMessage("id不能为空!");
            result.setSuccess(false);
            return result;
        }
        SeedMessageDTO apiDTO;
        try {
            List<String> idList = Arrays.asList(ids.split(","));
            apiDTO = baseService.removeByIds(idList);
        } catch (ServiceException e) {
            log.error("[api-v1]WebsiteNewsController.delete ", e);
            result.setCode("A0500");
            result.setSuccess(false);
            result.setMessage("删除失败");
            return result;
        }
        if (!apiDTO.getSuccess()) {
            result.setCode(apiDTO.getCode());
            result.setSuccess(false);
            result.setMessage(apiDTO.getMessage());
            return result;
        }
        return result;
    }


    @RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "根据id获取乌镇新闻详细信息")
    public WebsiteNewsApiDetailVO getDetailVO(@PathVariable @ApiParam(value = "主键id", required = true) String id) {
        log.info("[api-v1]WebsiteNewsController.getWebsiteNewsApiVO接口,根据id获取乌镇新闻详细信息");

        WebsiteNewsApiDetailVO result = new WebsiteNewsApiDetailVO();
        if (StringUtils.isEmpty(id)) {
            result.setCode("A0410");
            result.setMessage("id不能为空!");
            result.setSuccess(false);
            return result;
        }

        WebsiteNewsApiDetailData data;
        try {
            data = baseManager.convertApiDetailData(id);
        } catch (ManagerException e) {
            log.error("[api-v1]WebsiteNewsController.getWebsiteNewsApiVO接口 ", e);
            result.setCode("A0500");
            result.setSuccess(false);
            result.setMessage("查询失败");
            return result;
        }
        result.setData(data);
        return result;
    }


    @RequestMapping(value = "/all", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取全部")
    public WebsiteNewsApiDetailListVO listAll(@ApiParam(value = "查询条件", required = true) WebsiteNewsApiQuery query) {
        log.info("[api-v1]WebsiteNewsController.listAll,获取全部");
        WebsiteNewsApiDetailListVO result = new WebsiteNewsApiDetailListVO();
        if (StringUtils.isEmpty(query)) {
            query = new WebsiteNewsApiQuery();
        }
        try {
            result = baseService.listAll(query);
        } catch (ServiceException e) {
            log.error("[api-v1]WebsiteNewsController.listAll ", e);
            result.setCode("A0500");
            result.setSuccess(false);
            result.setMessage("查询失败");
            return result;
        }

        return result;
    }

    @RequestMapping(value = "/brief", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "根据查询条件查询简略对象")
    public WebsiteNewsApiBriefPageVO getBriefPageVO(@ApiParam(value = "查询条件", required = true) WebsiteNewsApiPageQuery query) {
        log.info("[api-v1]WebsiteNewsController.getBriefPageVO,根据查询条件查询简略对象");

        WebsiteNewsApiBriefPageVO result = new WebsiteNewsApiBriefPageVO();
        if (ObjectUtils.isEmpty(query)) {
            query = new WebsiteNewsApiPageQuery();
        }

        try {
            result = baseService.getBriefPageVO(query);
        } catch (ServiceException e) {
            log.error("[api-v1]WebsiteNewsController.getBriefPageVO ", e);
            result.setCode("A0500");
            result.setSuccess(false);
            result.setMessage("查询失败");
            return result;
        }

        return result;
    }


    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "根据查询条件查询详细对象")
    public WebsiteNewsApiDetailPageVO getDetailPageVO(@ApiParam(value = "查询条件", required = true) WebsiteNewsApiPageQuery query) {
        log.info("[api-v1]WebsiteNewsController.getDetailPageVO,根据查询条件查询详细对象");

        WebsiteNewsApiDetailPageVO result = new WebsiteNewsApiDetailPageVO();
        if (StringUtils.isEmpty(query)) {
            query = new WebsiteNewsApiPageQuery();
        }

        try {
            result = baseService.getDetailPageVO(query);
        } catch (ServiceException e) {
            log.error("[api-v1]WebsiteNewsController.getDetailPageVO ", e);
            result.setCode("A0500");
            result.setSuccess(false);
            result.setMessage("查询失败");
            return result;
        }

        return result;
    }

    /**
     * 根据标题模糊搜索
     *
     * @param title
     * @return
     */
    @RequestMapping(value = "/listByTitle", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "根据标题模糊搜索")
    public WebsiteNewsApiDetailListVO listByTitle(@RequestParam(value = "title") String title) {
        log.info("[api-v1]WebsiteNewsController.listByTitle,根据标题模糊搜索");

        WebsiteNewsApiDetailListVO result = new WebsiteNewsApiDetailListVO();
        if (org.apache.commons.lang3.StringUtils.isEmpty(title)) {
            result.setCode("A0500");
            result.setSuccess(false);
            result.setMessage("查询失败");
            return result;
        }

        try {
            result = baseService.listByTitle(title);
        } catch (ServiceException e) {
            log.error("[api-v1]WebsiteNewsController.listByTitle ", e);
            result.setCode("A0500");
            result.setSuccess(false);
            result.setMessage("查询失败");
            return result;
        }

        return result;
    }
}